import time
import moth
import urllib2

#Personalization Items
site = 'stackoverflow'
usrid = '68172'
timeframe = (60*60*24)
apiver = '1.0'
#End Personalization Items
#https://wiki.ubuntu.com/NotificationDevelopmentGuidelines

fromdate = int(time.time() - timeframe)
url = "http://api."+site+".com/"+apiver+"/"
api = moth.API(url)

try:
	userUri = "users/"+usrid
	userData = api.get(userUri)
	associationId=userData[u'users'][0][u'association_id']

	associatedUrl = "http://stackauth.com/"+apiver+"/users/"+associationId+"/associated"
	stackAuthApi = moth.API(associatedUrl)
	associatedData = stackAuthApi.get("")
	for siteData in associatedData[u'associated_users']:
		apiSite = moth.API(siteData[u'on_site'][u'api_endpoint']+"/"+apiver+"/")
		print "Checking site:",siteData[u'on_site'][u'name']
		usrid=siteData[u'user_id']
		uri = "users/"+str(usrid)+"/reputation?fromdate="+str(fromdate)
		repdata = apiSite.get(uri)
		if u'error' in repdata:
			print repdata[u'error'][u'message']
		elif u'rep_changes' in repdata:
			for reps in repdata[u'rep_changes']:
				print '[ +',int(reps[u'positive_rep']),', -',int(reps[u'negative_rep']),'] for:',reps[u'title']
		else:
			print 'No suitable response found'
except urllib2.HTTPError, e:
	print "Request failed",e.code
except urllib2.URLError, e:
	print e.reason()
